/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package LinkRetriever;

import DBSupport.DBApi;
import DataStructures.Project;
import General.Globals;
import General.Utilities;
import java.util.ArrayList;

/**
 *
 * @author Manuel
 */
public class DataManager {

    private static DataManager instance = new DataManager();


    private ArrayList<Link> links;
    private ArrayList<Project> projects;
    private DBApi api;
    private static int linkCounter;
    private int counter;

    public static DataManager getInstance(){
        return instance;
    }

    private DataManager(){
        linkCounter = 0;
        api = DBApi.getInstance();
        links = new ArrayList<Link>();
        projects = new ArrayList<Project>();
        counter = 0;
        this.updateLinks();
    }

    
    public synchronized Link getLink(){
        counter++;
        if(counter > Globals.LIST_UPDATE){
            this.updateLinks();
            counter = 0;
        }
        Link newl = null;
        Link updatel = null;
        Link tocheck = null;
        System.out.println("Checkpoint2, links:"+links.size());
         for(Link l : links){
             if(l.getStatus() == Globals.LINK_STATUS_NEW){
                 newl = l;
                 l.setStatus(Globals.LINK_STATUS_PROCESSING);
                 return l;
             }
             else if(l.getStatus() == Globals.LINK_STATUS_UPDATE && updatel == null){
                 updatel = l;
             }
             else if(l.getStatus() == Globals.LINK_STATUS_TOCHECK && updatel == null && tocheck == null){
                 tocheck = l;
             }

         }
         if(updatel != null){
          
             updatel.setStatus(Globals.LINK_STATUS_PROCESSING);
             return updatel;
         }
         tocheck.setStatus(Globals.LINK_STATUS_PROCESSING);
         return tocheck;
    }

    //updates the projets from the list scanning the database
    public synchronized void updateLinks(){
        //TODO choose alternating requests
        links = new ArrayList<Link>();
        ArrayList<String> dblinks = api.getAllInterestingLinks();
        for(String l : dblinks){
            String[] larr = l.split("-");
            //check which status the link has and add to the list
            if(larr.length == 3){
                if(larr[1].toLowerCase().contains("new"))
                    links.add(new Link(larr[0],Globals.LINK_STATUS_NEW,0));
                else if(larr[1].toLowerCase().contains("good"))
                    links.add(new Link(larr[0],Globals.LINK_STATUS_UPDATE,Integer.parseInt(larr[2])));
                else if(larr[1].toLowerCase().contains("tocheck"))
                    links.add(new Link(larr[0],Globals.LINK_STATUS_TOCHECK,Integer.parseInt(larr[2])));
            }

        }
    }

    public void storeProject(Project p){
        for(int i = 0; i< links.size();i++){
            Link l = links.get(i);
            if(l.getUrl().equals(p.getProjectInfo().getUrl()))
                links.remove(i);
        }
        api.updateProject(p);
    }


    private boolean linkExists(String s) {
        for(Link l : links){
            if(l.getUrl().toLowerCase().equals(s.toLowerCase()))
                return true;
        }
        return false;
    }

    public synchronized void freeLink(String url) {
        for(Link l : links){
            if(l.getUrl().equals(l))
                l.setStatus(Globals.LINK_STATUS_NEW);
        }
    }

  


}
